package de.lianowar.rolplayambiente;



import java.io.IOException;
import java.util.Properties;

import android.app.Activity;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import de.lianowar.rolplayambiente.soap.SoapConnection;


public class RolplayAmbienteActivity extends Activity {
    /** Called when the activity is first created. */
	
    private Button bu1;
    private Button bu2;
    private Button bu3;
    private Button bu4;
    private Button bu5;
    private Button bu6;
    private Button bu7;
    private Button bu8;
    private Button bu9;
    private Button bu10;
    
    private Boolean bu1check = false;
    private Boolean bu2check= false;
    private Boolean bu3check= false;
    private Boolean bu4check= false;
    private Boolean bu5check= false;
    private Boolean bu6check= false;
    private Boolean bu7check= false;
    private Boolean bu8check= false;
    private Boolean bu9check= false;
    private Boolean bu10check= false;
    
    private Button save;
    private Dialog changeip;
    private SharedPreferences.Editor editor;
    private SharedPreferences settings;
    private EditText ipadress;
    private SeekBar volume;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.main);
        creatComponents();        
       
        settings = getSharedPreferences("Config",MODE_PRIVATE); 
        editor = settings.edit();
        editor.putString("IP", "192.168.0.109");
        editor.commit();
        
        String test = settings.getString("IP","127.0.0.1");
        if(test.equals("127.0.0.1"))
        {

        	changeip = new Dialog(RolplayAmbienteActivity.this);
        	changeip.setContentView(R.layout.config);        	
        	ipadress = (EditText)changeip.findViewById(R.id.ipadress);
        	save = (Button)changeip.findViewById(R.id.save_ip);
        	ipadress.setText(test);

        	save.setOnClickListener(new OnClickListener() 
        	{    			    			
    			public void onClick(View v) 
    			{
    	        	editor.putString("IP", ipadress.getText().toString());
    	        	editor.commit();
    	        	changeip.cancel();
    			}	
    		});
        	changeip.show();
        }
        
     
        
        
        prepareActionListener();
    }
    
    private void sendMessage(String message, String _method)
    {
    	MyThread thread = new MyThread(message, _method);
    	thread.start();    	
    }
    private void sendVolume(String message, String _method)
    {
    	SetVolume thread = new SetVolume(message, _method);
    	thread.start();    	
    }
    private void creatComponents()
    {
   	
   	 	
        bu1 = (Button)findViewById(R.id.button1);
        bu2 = (Button)findViewById(R.id.button2);
        bu3 = (Button)findViewById(R.id.button3);
        bu4 = (Button)findViewById(R.id.button4);
        bu5 = (Button)findViewById(R.id.button5);
        bu6 = (Button)findViewById(R.id.button6);
        bu7 = (Button)findViewById(R.id.button7);
        bu8 = (Button)findViewById(R.id.button8);
        bu9 = (Button)findViewById(R.id.button9);
        bu10 = (Button)findViewById(R.id.button10);
        
   
   }
   private void prepareActionListener()
   {
   	 bu1.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) 
			{
				bu1check = StartStop(bu1check,"BUTTON1");
			}	
		});
   	 
    bu2.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) {
				bu2check = StartStop(bu2check,"BUTTON2");
			}	
		});
	 bu3.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) {
				bu3check = StartStop(bu3check,"BUTTON3");
			}	
		});
 bu4.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) {
				bu4check =  StartStop(bu4check,"BUTTON4");
			}	
		});
	 bu5.setOnClickListener(new OnClickListener() {
			
		
			public void onClick(View v) {
				bu5check = StartStop(bu5check,"BUTTON5");
			}	
		});
 bu6.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) {
				bu6check = StartStop(bu6check,"BUTTON6");
			}	
		});
	 bu7.setOnClickListener(new OnClickListener() {
			
		
			public void onClick(View v) {
				bu7check = StartStop(bu7check,"BUTTON7");
			}	
		});
bu8.setOnClickListener(new OnClickListener() {
			
		
			public void onClick(View v) {
				bu8check = StartStop(bu8check,"BUTTON8");
			}	
		});
bu9.setOnClickListener(new OnClickListener() {
	

	public void onClick(View v) {
		bu9check = StartStop(bu9check,"BUTTON9");
	}	
});
bu10.setOnClickListener(new OnClickListener() {
	

	public void onClick(View v) {
		bu10check = StartStop(bu10check,"BUTTON10");
	}	
});
   }
  private Boolean StartStop(Boolean _buCheck, String _Button)
  {
	Boolean temp = _buCheck;
   if(!_buCheck)
	{
	sendMessage(_Button,"PlaySound");
	temp = _buCheck = true;
	}
	else
	{
		sendMessage(_Button,"StopPlaySound");
		temp = _buCheck = false;
	}
   
   return temp;
  }
   class MyThread extends Thread
   {
	   
	    private SoapConnection test = new SoapConnection(settings.getString("IP","192.168.0.109"),"PlaySound");
	    private String message = "";
	    private String method = "";
	    public MyThread(String _messasge, String _method) {
	        this.message = _messasge;
	        this.method = _method;
	    }

	    public void run() 
	    {
	    	test.doRequest(this.message,this.method);
	    }
	}
   
   class SetVolume extends Thread
   {
	   
	    private SoapConnection test = new SoapConnection(settings.getString("IP","192.168.0.109"),"SetVolume");
	    private String message = "";
	    private String method = "";
	    public SetVolume(String _messasge,String _method) {
	        this.message = _messasge;
	        this.method = _method;
	    }

	    public void run() 
	    {
	    	test.doRequest(this.message, this.method);
	    }
	}
}